﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

[RequireComponent(typeof(UIType))]
[RequireComponent(typeof(EventTriggerListener))]
public class UIButtonExtension : Button
{
#if UNITY_EDITOR
    [MenuItem("GameObject/UI/ButtonEx")]
    static void CreateButtonEx(MenuCommand menuCmd)
    {
        // 创建游戏对象
        float w = 160f;
        float h = 30f;
        GameObject btnRoot = UICommon.CreateUIElementRoot("ButtonEx", w, h);

        // 创建Text对象
        UICommon.CreateUIText("Text", "Button", btnRoot);

        // 添加脚本
        btnRoot.AddComponent<CanvasRenderer>();
        Image img = btnRoot.AddComponent<Image>();
        img.color = Color.white;
        img.fillCenter = true;
        img.raycastTarget = true;
        img.sprite = UICommon.FindRes<Sprite>("UISprite");
        if (img.sprite != null)
            img.type = Image.Type.Sliced;

        btnRoot.AddComponent<UIButtonExtension>();
        btnRoot.GetComponent<Selectable>().image = img;

        // 放入到UI Canvas中
        UICommon.PlaceUIElementRoot(btnRoot, menuCmd);
    }

#endif

    // 构造函数
    protected UIButtonExtension()
    {
        my_onDoubleClick = new ButtonClickedEvent();
       
    }

   
    
    public Action<bool> OnButtonUp;

    public Action OnButtonDown;

    // 双击
    public ButtonClickedEvent my_onDoubleClick;
    public ButtonClickedEvent OnDoubleClick
    {
        get { return my_onDoubleClick; }
        set { my_onDoubleClick = value; }
    }
    

    // 长按需要的变量参数
    private bool my_isStartPress = false;
    private float my_curPointDownTime = 0f;
    private float my_longPressTime = 0.6f;
    private bool my_longPressTrigger = false;


    void Update()
    {
        CheckIsLongPress();
    }

    #region 长按

    /// <summary>
    /// 处理长按
    /// </summary>
    void CheckIsLongPress()
    {
        if (my_isStartPress && !my_longPressTrigger)
        {
            if (Time.time > my_curPointDownTime + my_longPressTime)
            {
                my_longPressTrigger = true;
                my_isStartPress = false;
               
            }
        }

      
    }

    public override void OnPointerEnter(PointerEventData eventData)
    {
        base.OnPointerEnter(eventData);

    }

    public override void OnPointerDown(PointerEventData eventData)
    {
        // 按下刷新當前時間
        base.OnPointerDown(eventData);
        my_curPointDownTime = Time.time;
        if (OnButtonDown != null)
        {
            OnButtonDown();
        }
        my_isStartPress = true;
        my_longPressTrigger = false;
    }

    public override void OnPointerUp(PointerEventData eventData)
    {
        // 指針擡起，結束開始長按
        base.OnPointerUp(eventData);
        if(OnButtonUp != null)
        {
            OnButtonUp(my_longPressTrigger);
        }
        my_isStartPress = false;

    }

    public override void OnPointerExit(PointerEventData eventData)
    {
        // 指針移出，結束開始長按，計時長按標志
        base.OnPointerExit(eventData);
        my_isStartPress = false;

    }

    #endregion

    #region 多次点击（单击）

    public override void OnPointerClick(PointerEventData eventData)
    {
        //(避免已經點擊進入長按后，擡起的情況)
        if (!my_longPressTrigger)
        {
            // 正常單擊 

            switch(eventData.clickCount)
            {             
                case 2:
                    if (my_onDoubleClick != null)
                    {
                        my_onDoubleClick.Invoke();
                    }
                    break;
                case 1:
                    onClick.Invoke();
                    break;
            }
            
        }
    }
    #endregion


}
